package com.wmlt.service.impl;

import com.wmlt.util.JdbcUtil;
import com.wmlt.dao.UserDao;
import com.wmlt.dao.impl.UserDaoImpl;
import com.wmlt.entity.Article;
import com.wmlt.entity.User;
import com.wmlt.service.UserService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    User user = null;
    int result = 0;//0未注册，1已注册
    Connection connection = null;

    @Override
    public User qqRegister(User user) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = userDao.qqRegister(user);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {

            JdbcUtil.close(null, null, connection);
        }
        return user;
    }

    @Override
    public User mobileLoginAndRegister(String mobile) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = userDao.mobileLoginAndRegister(mobile);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {

            JdbcUtil.close(null, null, connection);
        }
        return user;
    }

    @Override
    public User adminLogin(String userName, String password) {

        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = new User();
            user = userDao.adminLogin(userName, password);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, null);
        }
        return user;
    }

    @Override
    public int getUserNumber() {
        return userDao.getUserNumber();
    }

    @Override
    public List<User> getAllUserInfo(String username, int page, int limit) {
        return userDao.getAllUserInfo(username, page, limit);
    }

    @Override
    public int deleteUserInfo(int[] userId) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = new User();
            result = userDao.deleteUserInfo(userId);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public User getUserInfoByUserId(String userId) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = new User();
            user= userDao.getUserInfoByUserId(userId);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return user;
    }

    @Override
    public int changePassword(String userId, String password) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            user = new User();
            result = userDao.changePassword(userId, password);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }
    //根据文章id拿到内容

    @Override
    public List<Article> getUserIdInfoByUser(String articleId) {
        return userDao.getUserIdInfoByUser(articleId);
    }
    //用户修改信息

    @Override
    public int updateUserManager(String userId, User user) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = userDao.updateUserManager(userId, user);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public User mobileAndPassLogin(String mobile, String pass) {
        return userDao.mobileAndPassLogin(mobile, pass);
    }

    @Override
    public int addUserCb(String userId, int cb) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = userDao.addUserCb(userId, cb);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public int getUserCbByUserId(String userId) {
        return userDao.getUserCbByUserId(userId);
    }
}
